package com.my.study.utils;

import com.my.study.config.EmailConfig;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class SendEmailUtil {

    private EmailConfig emailConfig;

    private MailSender mailSender;

    private void SendEmailUtil() {
    }

    public static SendEmailUtil getInstance(EmailConfig emailConfig, MailSender mailSender) {
        SendEmailUtil sendEmailUtil = new SendEmailUtil();
        sendEmailUtil.emailConfig = emailConfig;
        sendEmailUtil.mailSender = mailSender;
        return sendEmailUtil;
    }

    /**
     * 发送邮件
     *
     * @param title
     * @param text
     * @return
     */
    public boolean sendEmail(String title, String text) {
        //发送通知邮件
        SimpleMailMessage message = new SimpleMailMessage();
        //设置邮件标题
        message.setSubject(title);
        message.setText(text);
        //设置收件人，测试
        message.setTo(emailConfig.getUsername());
        //设置发件人，测试
        message.setFrom(emailConfig.getUsername());
        //发送邮件
        mailSender.send(message);
        return true;
    }
}
